﻿
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace BugAdmin.Core.Extension
{
    public static class SwaggerExt
    {
        /// <summary>
        /// 添加Swagger
        /// </summary>
        /// <param name="services"></param>
        /// <param name="DocTitle">文档名称</param>
        /// <param name="description">文档描述</param>
        public static void AddSwaggerExt(this IServiceCollection services,string DocTitle,string description)
        {
            services.AddEndpointsApiExplorer();
            services.AddSwaggerGen(option =>
            {
                foreach (var v in typeof(ApiVersion).GetEnumNames())
                {
                    option.SwaggerDoc(v, new OpenApiInfo()
                    {
                        Title = DocTitle,
                        Version = v,
                        Description = $"{description}{v}",
                    });
                }
                //xml文档绝对路径
                var file = Path.Combine(AppContext.BaseDirectory, $"{AppDomain.CurrentDomain.FriendlyName}.xml ");
                //true:显示控制器层注释
                option.IncludeXmlComments(file, true);
                // 对action的名称进行排序，如果有多个，就可以看见效果了。
                option.OrderActionsBy(o => o.RelativePath);
                #region jwt Token
                //添加安全定义-配置支持token授权机制
                option.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
                {
                    Description = "请输入token,格式为Bearer xxxxxxxx(注意中间必须有空格)",
                    Name = "Author ization",
                    In = ParameterLocation.Header,
                    Type = SecuritySchemeType.ApiKey,
                    BearerFormat = "JWT",
                    Scheme = "Bearer"
                });
                //添加安全要求
                var securityRequirement = new OpenApiSecurityRequirement
                 {
                    {
                        new OpenApiSecurityScheme
                        {
                            Reference = new OpenApiReference
                            {
                                Type = ReferenceType.SecurityScheme,
                                Id = "token"
                            }
                        },
                        new string[] {}
                    }
                 };
                #endregion
            });
        }
        /// <summary>
        /// 使用Swagger
        /// </summary>
        /// <param name="app"></param>
        /// <param name="DocTitle">文档名称</param>
        public static void UserSwaggerExt(this WebApplication app,string DocTitle)
        {
            app.UseSwagger();
            app.UseSwaggerUI(option =>
            {
                foreach (var v in typeof(ApiVersion).GetEnumNames())
                {
                    option.SwaggerEndpoint($"/swagger/{v}/swagger.json", $"{DocTitle}【{v}】版本");
                }
            });
        }
    }
}
